import Vue from 'vue'
import messageBox from './index.vue'

const messageConstructor = Vue.extend(messageBox)
let instanceConfig = null
messageBox.install = (message, title, options = {}, messageType) => {
  if (typeof title === 'object') {
    options = title
    title = ''
  } else if (title === undefined) {
    title = ''
  }
  options = Object.assign({
    title: title,
    message: message,
    messageType
  }, options)

  if (instanceConfig) {
    return instanceConfig.visibleTouch()
  }
  let instance = new messageConstructor({
    data: options
  }).$mount()
  document.body.appendChild(instance.$el)
  if (!instanceConfig) {
    instanceConfig = instance
  }
  return instance.visibleTouch()
}
const confirm = (message, title, options) => messageBox.install(message, title, options, 'confirm')
const alert = (message, title, options) => messageBox.install(message, title, options, 'alert')

function MessageBox (message, title, options) {
  alert(message, title, options)
}
Vue.prototype.$confirm = confirm
Vue.prototype.$alert = alert
MessageBox.alert = alert
MessageBox.confirm = confirm

export default MessageBox
// 缺prompt
